package com.cm367.teamc.calendar;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 



import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
 

import javax.faces.bean.ManagedProperty;

import org.richfaces.model.CalendarDataModel;
import org.richfaces.model.CalendarDataModelItem;

import com.cm367.teamc.dao.PublicHolidayDAO;
import com.cm367.teamc.util.DateUtil;
 
@ManagedBean
@ApplicationScoped
public class CalendarPublicHolidayWeekEnd implements CalendarDataModel {
	
	@ManagedProperty(value = "#{publicHolidayDAO}")
	private PublicHolidayDAO publicHolidayDAO;
	
    public PublicHolidayDAO getPublicHolidayDAO() {
		return publicHolidayDAO;
	}

	public void setPublicHolidayDAO(PublicHolidayDAO publicHolidayDAO) {
		this.publicHolidayDAO = publicHolidayDAO;
	}

	private static final String WEEKEND_DAY_CLASS = "wdc";
    private static final String PUBLIC_HOILDAY_CLASS = "phc";
    private static final String BOUNDARY_DAY_CLASS = "rf-ca-boundary-dates";
    private static final String NORMAL_CLASS = "normal-dates";
 
    private boolean checkPublicHoliday(Calendar calendar) throws SQLException {
    	List<String> publicHoildays = publicHolidayDAO.getPublicHoliday();
    	if (publicHoildays.contains(DateUtil.getStringDate(calendar.getTime())))
    		return true;
    	else
    		return false;
    }
 
    private boolean checkWeekend(Calendar calendar) {
        return (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY);
    }
 
    public CalendarDataModelItem[] getData(Date[] dateArray) {
        CalendarDataModelItem[] modelItems = new CalendarModelItem[dateArray.length];
        Calendar current = GregorianCalendar.getInstance();
        Calendar yearStart = GregorianCalendar.getInstance();
        Calendar yearEnd = GregorianCalendar.getInstance();
        Calendar today = GregorianCalendar.getInstance();
        today.setTime(new Date());
        int year = today.get(Calendar.YEAR);        
		yearStart.set(year-1, 11,31);
		yearEnd.set(year, 11, 31);
        for (int i = 0; i < dateArray.length; i++) {
            current.setTime(dateArray[i]);
            CalendarModelItem modelItem = new CalendarModelItem();
            try {
            	if (current.before(yearStart) || current.after(yearEnd)){
            		modelItem.setEnabled(false);
                    modelItem.setStyleClass(BOUNDARY_DAY_CLASS);
            	}
            	else if (checkPublicHoliday(current)) {
				    modelItem.setEnabled(false);
				    modelItem.setStyleClass(PUBLIC_HOILDAY_CLASS);
				} else if (checkWeekend(current)) {
				    modelItem.setEnabled(false);
				    modelItem.setStyleClass(WEEKEND_DAY_CLASS);
				} else {
				    modelItem.setEnabled(true);
				    modelItem.setStyleClass(NORMAL_CLASS);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            modelItems[i] = modelItem;
        }
 
        return modelItems;
    }
 
    public Object getToolTip(Date date) {
        // TODO Auto-generated method stub
        return null;
    }
}